/**
 * 聊天
 */
function chat() {
    this.oAlert = $("#js_alert");
    this.oMsg = this.oAlert.find(".num");
    this.oIcon = this.oAlert.find(".icon");

    this.oChatBox = $("#js_chatbox");
    this.sendReq = null; //发送请求
    this.init();
    return this;
};
chat.prototype = {
    init: function() {
        var global = this,
            _bd = global.oChatBox.find(".tab-bd");
        if (_bd.size() < 1) {
            global.oAlert.addClass("z-dis");
        }
        //点击查看聊天框
        this.oAlert.on("click", function() {
            global.showChat();
        });
        //关闭聊天框
        this.oChatBox.on("click", ".tab-bd .close", function() {
            if (confirm("确定要关闭所有聊天吗？")) {
                global.closeChat();
            }
        });

        //显示 
        this.showChat();
        //表情符下拉框
        _bd.find(".js_face").faceedit();
        //滚动条
        _bd.find(".msg-list").rollbar({ zIndex: 80, pathPadding: 0 });
    },
    receiveMsg: function(user,msg) {
        //接收消息
        var _str = '<li class="ask">' +
            '    <i class="avatar">'+user+'</i>' +
            '    <div class="wrap">' +
            '        <div class="content">' + (msg || '-') + '</div>' +
            '    </div>' +
            '</li>';
        try {
            this.oChatBox.find(".msg-list ul").append(_str);
            this.scrollToBottom();
        } catch (e) {
            console.info(e);
        }
        //计算消息条数
        var _totalMsg = this.oMsg.html();
        _totalMsg++;
        this.oMsg.html(_totalMsg);
    },
    sendMsg: function(callback) {
        var global = this;
        //发送动作
        global.oChatBox.find(".js_form").on("submit", function(e) {
            e.preventDefault();
            var _msg = $.trim(global.oChatBox.find(".js_form").find("input").val());
            if (!_msg) {
                alert("请输入聊天内容");
                return false;
            }
            var _str;
            if (callback && typeof(callback) == 'function') {
                callback.call(global, _msg);
            }
            var _str = '<li class="answer">' +
                '    <i class="avatar">Sd</i>' +
                '    <div class="wrap">' +
                '        <div class="content">' + _msg + '</div>' +
                '    </div>' +
                '</li>';
            global.oChatBox.find(".msg-list ul").append(_str);
            global.oChatBox.find(".js_ipt").val(""); //清空内容
            global.scrollToBottom();
        });
    },
    receiveSystem: function(msg) {
        this.oChatBox.find(".tab-bd").eq(0).find(".msg-list ul").append('<li class="timeline">' + msg + '</li>');
    },
    scrollToBottom: function() {
        var wrapHeight = this.oChatBox.find(".msg-list").height();
        var innerHeight = this.oChatBox.find(".msg-list ul").parent().height();
        if (innerHeight > wrapHeight) {
            this.oChatBox.find(".msg-list ul").parent().animate({ "top": (wrapHeight - innerHeight) });
        }
    },
    showChat: function() {
        var global = this;
        this.oChatBox.fadeIn();
        this.oAlert.hide();
    },
    closeChat: function() {
        this.oChatBox.fadeOut();
        this.oMsg.html(0);
        this.oIcon.removeClass("active");
        this.oAlert.show();

    },
    addUser: function({sid,user}) {
        this.oChatBox.find(".group").find('ul').append('<li data-id="' + sid + '"><a href="javascript:;"><span class="avatar">' + user + '</span>' + user + '</a></li>');
    },
    removeUser: function(sid) {
        var global = this;
        global.oChatBox.find(".group").find("li").each(function(idx, itm) {
            if (($(itm).data('id') == sid)) {
                $(itm).remove();
            }
        })
    }
};
/**
 * 监听
 */
$(function() {
    //实例化
    var socket = io();
    var nickname = "default" || prompt("请输入您的昵称", "");
    //实例化聊天
    var _chat = new chat();
    
    //事件
    socket.emit("join", {
        user: nickname,
        head: "",
        nickname: nickname
    })
    //监听:
    socket.on("connect", function() {
        //广播登录
        socket.emit('system', {
            user: nickname,
            msg: '欢迎加入' + nickname
        })

        //系统消息
        socket.on("system", function({sid, user, leave, msg }) {
            console.log('system:' + msg);
            if (user) {
                _chat.addUser({sid,user});
            } else if (leave) {
                _chat.removeUser(sid);
            }
            _chat.receiveSystem(msg);
        });
        //普通消息
        socket.on("msg", function({user,msg}) {
            console.log('normal msg');
            _chat.receiveMsg(user,msg);
        });


        _chat.sendMsg(function(msg){
              socket.emit('msg',msg)
        });
    });

});


//操作组员
function opt(obj) {
    var obj = $(obj);
    var _html = $(ui.opt());
    obj.parent().append(_html);

    //outclick事件
    _html.on("outerClick", function() {
        _html.remove();
    });
}


//test3
